package com.lianchuang.util;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class StaticResourceDownload {

	public static Document getDoc(String url) throws Exception {
		return Jsoup.connect(url).header("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2").get();
	}

	public static String getHtmlForNet(String url) throws Exception {
		return getDoc(url).html().replaceAll("最代码", "代码园");
	}
	public static  String downFile(String url,String savePath,String fileName) throws Exception{
		try {
			String type = url.substring(url.lastIndexOf("."));
			StaticResourceDownload.downloadFileFromUrl(url, fileName+type, savePath);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
	public static void getLinks(String url) throws IOException {
		Document doc = Jsoup.connect(url).get();
		Elements links = doc.select("a[href]");
		Elements media = doc.select("[src]");
		Elements imports = doc.select("link[href]");

		print("\nMedia: (%d)", media.size());
		for (Element src : media) {
			if (src.tagName().equals("img"))
				print(" * %s: <%s> %sx%s (%s)", src.tagName(), src.attr("abs:src"), src.attr("width"), src.attr("height"), trim(src.attr("alt"), 20));
			else
				print(" * %s: <%s>", src.tagName(), src.attr("abs:src"));
		}

		print("\nImports: (%d)", imports.size());
		for (Element link : imports) {
			print(" * %s <%s> (%s)", link.tagName(), link.attr("abs:href"), link.attr("rel"));
		}

		print("\nLinks: (%d)", links.size());
		for (Element link : links) {
			print(" * a: <%s>  (%s)", link.attr("abs:href"), trim(link.text(), 35));
		}
	}

	public static void getPageCss(String urlStr, String savePath) throws Exception {
		Document doc = Jsoup.connect(urlStr).get();
		Elements imports = doc.select("link[href]");
		for (Element link : imports) {
			String url = link.attr("abs:href");
			if (url.contains(".css")) {
				downloadFromUrl(url, savePath);
			}
		}
	}

	public static void getPageJs(String urlStr, String savePath) throws Exception {
		Document doc = Jsoup.connect(urlStr).get();
		Elements media = doc.select("[src]");
		for (Element src : media) {
			if (src.tagName().equals("script")) {
				String jsUrl = src.attr("abs:src");
				if (jsUrl.contains("baidu") || jsUrl.contains("google") || jsUrl.contains("taobao")) {
					continue;
				}
				downloadFromUrl(jsUrl, savePath);
			}
		}
	}

	private static void print(String msg, Object... args) {
		System.out.println(String.format(msg, args));
	}

	private static String trim(String s, int width) {
		if (s.length() > width)
			return s.substring(0, width - 1) + ".";
		else
			return s;
	}

	private static void downloadFromUrl(String urlStr, String savePath) throws Exception {
		downloadFileFromUrl(urlStr, getFileNameFromUrl(urlStr), savePath);
	}

	/**
	 * 从url中截取文件名称
	 * 
	 * @return
	 */
	private static String getFileNameFromUrl(String urlStr) {
		String name = urlStr.substring(urlStr.lastIndexOf("/"));
		if (name.contains("?")) {
			return name.substring(0, name.indexOf("?"));
		} else {
			return name;
		}
	}

	public static void downloadFromUrl(String urlStr, String fileName, String savePath) throws Exception {
		URL url = new URL(urlStr);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		// 设置超时间为3秒
		conn.setConnectTimeout(15 * 1000);
		// 防止屏蔽程序抓取而返回403错误
		conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
		// 得到输入流
		InputStream inputStream = conn.getInputStream();
		// 获取自己数组
		// byte[] getData = readInputStream(inputStream);
		byte[] getData = readInputStreamByHtml(getHtmlForNet(urlStr));
		BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
		// 文件保存位置
		File saveDir = new File(savePath);
		if (!saveDir.exists()) {
			saveDir.mkdirs();
		}
		File file = new File(saveDir + File.separator + fileName);
		FileOutputStream fos = new FileOutputStream(file);
		fos.write(getData);
		if (fos != null) {
			fos.close();
		}
		if (inputStream != null) {
			inputStream.close();
		}

		System.out.println("info:" + url + " download success");
	}

	/**
	 * 下载文件到指定位置
	 * 
	 * @param urlStr
	 * @param fileName
	 * @param savePath
	 * @throws Exception
	 */
	public static void downloadFileFromUrl(String urlStr, String fileName, String savePath) throws Exception {
		File saveDir = new File(savePath);
		if (!saveDir.exists()) {
			saveDir.mkdirs();
		}
		File file = new File(saveDir + File.separator + fileName);
		if (file.exists()) {
			System.out.println(file.getAbsolutePath() + "已存在");
			return;
		}
		URL url = new URL(urlStr);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		// 设置超时间为3秒
		conn.setConnectTimeout(5 * 1000);
		// 防止屏蔽程序抓取而返回403错误
		conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
		// 得到输入流
		InputStream inputStream = conn.getInputStream();
		// 获取自己数组
		byte[] getData = readInputStream(inputStream);
		BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
		// 文件保存位置
		FileOutputStream fos = new FileOutputStream(file);
		fos.write(getData);
		if (fos != null) {
			fos.close();
		}
		if (inputStream != null) {
			inputStream.close();
		}
		saveDir.setReadable(true, false);
		file.setReadable(true, false);
		System.out.println("info:" + url + " download success");
	}

	public static void stringToFile(String html, String fileName, String savePath) throws Exception {
		byte[] getData = html.getBytes();
		// BufferedReader br = new BufferedReader(new
		// InputStreamReader(inputStream,"UTF-8"));
		// 文件保存位置
		File saveDir = new File(savePath);
		if (!saveDir.exists()) {
			saveDir.mkdirs();
		}
		File file = new File(savePath + File.separator + fileName);
		if(file.exists()){
			file.createNewFile();
		}
		FileOutputStream fos = new FileOutputStream(file);
		fos.write(getData);
		if (fos != null) {
			fos.close();
		}
		file.setReadable(true, false);
	}

	private static byte[] readInputStreamByHtml(String html) {
		return html.getBytes();
	}

	/**
	 * 从输入流中获取字节数组
	 * 
	 * @param inputStream
	 * @return
	 * @throws IOException
	 */
	private static byte[] readInputStream(InputStream inputStream) throws IOException {
		byte[] buffer = new byte[1024];
		int len = 0;
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		while ((len = inputStream.read(buffer)) != -1) {
			bos.write(buffer, 0, len);
		}
		bos.close();
		return bos.toByteArray();
	}

	public static void downloadFromWeb(String urlString, String filename) throws Exception {
		// 构造URL
		URL url = new URL(urlString);
		// 打开连接
		URLConnection con = url.openConnection();
		// 输入流
		InputStream is = con.getInputStream();
		// 1K的数据缓冲
		byte[] bs = new byte[1024];
		// 读取到的数据长度
		int len;
		// 输出的文件流
		OutputStream os = new FileOutputStream(filename);
		// 开始读取
		while ((len = is.read(bs)) != -1) {
			os.write(bs, 0, len);
		}
		// 完毕，关闭所有链接
		os.close();
		is.close();
	}
	
}
